package com.learn.project2.service.wx.user;

import com.learn.project2.bean.*;
import com.learn.project2.mapper.MarketGoodsMapper;
import com.learn.project2.mapper.MarketOrderMapper;
import com.learn.project2.mapper.MarketUserMapper;
import com.learn.project2.model.bo.wx.auth.AuthLoginBO2;
import com.learn.project2.model.bo.wx.user.UserIndexBO2;
import com.learn.project2.model.vo.wx.user.UserIndexVO2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * \* Author: Valley
 * \* Date: 2022/1/8
 * \* Time: 21:06
 * \* Description:
 */
@Service
public class UserServiceImpl2 implements UserService2 {

    @Autowired
    MarketOrderMapper orderMapper;
    @Autowired
    MarketUserMapper userMapper;

    /**
     * @description: 用户主页
     * @param: []
     * @return: com.learn.project2.model.vo.wx.user.UserIndexVO2
     * @author 王力
     * @date: 2022-01-10 22:48:05
     */
    @Override
    public UserIndexVO2 userIndex() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        MarketUserExample userExample = new MarketUserExample();
        userExample.createCriteria()
                .andUsernameEqualTo(principal.getUsername())
                .andPasswordEqualTo(principal.getPassword()).andDeletedEqualTo(false);
        List<MarketUser> marketUsers = userMapper.selectByExample(userExample);

        UserIndexVO2 userIndexVO2 = new UserIndexVO2();
        UserIndexVO2.OrderBean orderBean = new UserIndexVO2.OrderBean();

        MarketOrderExample orderExample = new MarketOrderExample();
        orderExample.createCriteria()
                .andUserIdEqualTo(marketUsers.get(0).getId())
                .andOrderStatusBetween((short)200,(short)204)
                .andShipSnIsNull().andDeletedEqualTo(false);
        List<MarketOrder> marketOrders = orderMapper.selectByExample(orderExample);
        orderBean.setUnship(marketOrders.size());


        MarketOrderExample orderExample2 = new MarketOrderExample();
        orderExample2.createCriteria()
                .andUserIdEqualTo(marketUsers.get(0).getId())
                .andOrderStatusGreaterThan((short)400)
                .andCommentsGreaterThan((short)0).andDeletedEqualTo(false);
        List<MarketOrder> marketOrders2 = orderMapper.selectByExample(orderExample2);
        Integer count = 0;
        for (MarketOrder order : marketOrders2) {
            count += order.getComments();
        }
        orderBean.setUncomment(count);

        MarketOrderExample orderExample3 = new MarketOrderExample();
        orderExample3.createCriteria()
                .andUserIdEqualTo(marketUsers.get(0).getId())
                .andOrderStatusEqualTo((short)101).andDeletedEqualTo(false);
        List<MarketOrder> marketOrders3 = orderMapper.selectByExample(orderExample3);
        orderBean.setUnpaid(marketOrders3.size());

        MarketOrderExample orderExample4 = new MarketOrderExample();
        orderExample4.createCriteria()
                .andUserIdEqualTo(marketUsers.get(0).getId())
                .andOrderStatusLessThan((short)400)
                .andShipSnIsNotNull().andDeletedEqualTo(false);
        List<MarketOrder> marketOrders4 = orderMapper.selectByExample(orderExample4);
        orderBean.setUnrecv(marketOrders4.size());

        userIndexVO2.setOrder(orderBean);

        return userIndexVO2;
    }
}
